package org.aisino.shdzfp.vistool.swing;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.util.zip.ZipException;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.aisino.shdzfp.vistool.exception.PasswordException;
import org.aisino.shdzfp.vistool.exception.WrongTripleFormatException;
import org.aisino.shdzfp.vistool.util.AnaMsg;
import org.aisino.shdzfp.vistool.util.EncryptionMsg;
import org.apache.log4j.Logger;

public class MsgPanel extends JPanel {

	private final static Logger log = Logger.getLogger(MsgPanel.class);

	private JButton buttonEncr;
	private JCheckBox[] boxs;
	private JTextField textPwd;
	private JButton buttonAna;
	private JButton buttonCopy;
	public static JTextArea areaOrigin;
	public static JTextArea areaResult;

	public MsgPanel() {
		paint();
		setMonitor();
	}

	private void paint() {
		this.setLayout(null);
		
		int margin = 5;
		
		int marginX = 5;
		int marginY = 5;
		int widthOrigin = 632;
		int heightOrigin = 180;

		int currX = marginX;
		int currY = marginY;
		int moduleY = marginY;
		//module a
		areaOrigin = new JTextArea();
		JScrollPane scrollPlainText = new JScrollPane(areaOrigin);
		scrollPlainText.setBounds(marginX, marginY, widthOrigin, heightOrigin);
		scrollPlainText.setAutoscrolls(true);
		add(scrollPlainText);
		
		
		//module b
		currX = 0;
		currY = moduleY + heightOrigin;
		int boxEX = 75;
		int boxEY = 30;
		moduleY = boxEY;
		boxs = setCheckBox(this);
		boxs[0].setBounds(currX = marginX, currY, boxEX, boxEY);
		boxs[1].setBounds(currX += boxEX, currY, boxEX, boxEY);
		boxs[2].setBounds(currX += boxEX, currY, boxEX, boxEY);
		

		int labelX = 30;
		int labelY = boxEY;
		int textX = 150;
		int textY = boxEY;
		JLabel labelPwd = new JLabel("Pwd:");
		textPwd = new JTextField(15);
		labelPwd.add(textPwd);
		labelPwd.setBounds(currX += boxEX, currY, labelX, labelY);
		textPwd.setBounds(currX += labelX, currY, textX, textY);
		add(labelPwd);
		add(textPwd);

		int buttonX = 70;
		int buttonY = 30;
		buttonEncr = new JButton("Encode");
		buttonAna = new JButton("Decode");
		buttonEncr.setBounds(currX += (textX + margin), currY, buttonX, buttonY);
		buttonAna.setBounds(currX += (buttonX + margin), currY, buttonX, buttonY);
		add(buttonEncr);
		add(buttonAna);

		int longButtonX = 70;
		int longButtonY = buttonY;
		buttonCopy = new JButton("Copy");
		buttonCopy.setBounds(currX += (buttonX + margin), currY, longButtonX, longButtonY);
		add(buttonCopy);

		//module c
		currX = marginX;
		areaResult = new JTextArea(10, 60);
		JScrollPane scrollResult = new JScrollPane(areaResult);
		scrollResult.setBounds(currX, currY += (moduleY + margin), widthOrigin, heightOrigin);
		scrollResult.setAutoscrolls(true);
		add(scrollResult);

	}

	private JCheckBox[] setCheckBox(JPanel panel) {
		// 创建控件文本数组
		String[] labels = { "Base64", "3DES", "Gzip" };
		JCheckBox[] boxs = new JCheckBox[labels.length];// 创建控件数组
		for (int i = 0; i < boxs.length; i++) {// 遍历控件数组
			if (i == 0) {
				boxs[i] = new JCheckBox(labels[i], true);
				boxs[i].setEnabled(false);
			} else {
				boxs[i] = new JCheckBox(labels[i]);// 初始化数组中的复选框组件
			}
			panel.add(boxs[i]);// 把数组元素（即每个复选框）添加到面板中
		}
		return boxs;
	}
	
	private void broadcastError(String msg) {
		areaResult.setForeground(Color.RED);
		areaResult.setText(msg);
	}
	
	private void broadcastInfo(String msg) {
		areaResult.setForeground(Color.BLACK);
		areaResult.setText(msg);
	}
	
	private int getBoxsValue() {
		int val = 0;
		if (boxs[0].isSelected()) {
			val += 1;
		}
		if (boxs[1].isSelected()) {
			val += 2;
		}
		if (boxs[2].isSelected()) {
			val += 4;
		}
		return val;
	}

	private void setMonitor() {
		// 加密
		buttonEncr.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int val = getBoxsValue();

				String result = "";
				try {
					switch (val) {
					case 1:
						result = EncryptionMsg.Base64_Encrypt(areaOrigin.getText());
						break;
					case 3:
						result = EncryptionMsg.TripleDES_Base64_Encrypt(textPwd.getText().trim(), areaOrigin.getText());
						break;
					case 5:
						result = EncryptionMsg.Gzip_Base64_Enycrypt(areaOrigin.getText());
						break;
					case 7:
						result = EncryptionMsg.TripleDES_Gzip_Base64_Encrypt(textPwd.getText().trim(), areaOrigin.getText());
						break;
					default:
						result = "ERROR!!";
					}
					areaResult.setForeground(Color.BLACK);
					areaResult.setText(result);
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch(ZipException e1) {
					e1.printStackTrace();
					broadcastError("ERROR : Not in GZIP format");
				} catch(PasswordException e1) {
					e1.printStackTrace();
					broadcastError("ERROR : WRONG PASSWORD");
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		// 解码
		buttonAna.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int val = getBoxsValue();

				try {
					String result = "";
					switch (val) {
					case 1:
						result = AnaMsg.Base64_Ana(areaOrigin.getText());
						break;
					case 3:
						result = AnaMsg.TripleDES_Base64_Ana(textPwd.getText().trim(), areaOrigin.getText());
						break;
					case 5:
						result = AnaMsg.Gzip_Base64_Ana(areaOrigin.getText());
						break;
					case 7:
						result = AnaMsg.TripleDES_Gzip_Base64_Ana(textPwd.getText().trim(), areaOrigin.getText());
						break;
					default:
						result = "ERROR!!";
					}
					broadcastInfo(result);
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (PasswordException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					broadcastError("ERROR : WRONG PASSWORD");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					if(e1.getClass() == java.util.zip.ZipException.class) {
						broadcastError("ERROR : Not In GZip Format");
					} else {
						System.out.println("%%%%%%%%%%%%%%%%%%%%");
					}
					broadcastError("ERROR : " + e1.getMessage());
				} catch (WrongTripleFormatException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					broadcastError("ERROR : Not In 3DES Format");
				} 
			}
		});

		buttonCopy.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				StringSelection stsel = new StringSelection(areaResult.getText());
				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
			}
		});
	}
}
